Random Module in Python

Manish Patel

2023-07-16

RANDOM MODULE

The random module in Python provides functions for generating random numbers and making random selections from sequences.

1. seed():

  • The seed() function is used to initialize the random number generator with a specific seed value.
  • By setting a seed value, you can ensure that the random numbers generated are repeatable.

Example:

import random

# Set the seed to 42
random.seed(42)

# Generate random numbers
print(random.random())   
print(random.random())  

2. randint(a, b):

  • The randint() function returns a random integer between the specified inclusive range [a, b].

Example:

import random

# Generate a random integer between 1 and 10
num = random.randint(1, 10)
print(num)   # Output: (random integer between 1 and 10)

3. random():

  • The random() function returns a random float between 0 and 1(not included).

Example:

import random

# Generate a random float between 0 and 1(not included)
num = random.random()
print(num)   # Output: (random float between 0 and 1(not included))

4. choice(seq):

  • The choice() function returns a random element from the given sequence seq.

Example:

import random

# Randomly select a color from a list
colors = ['red', 'blue', 'green', 'yellow']
selected_color = random.choice(colors)
print(selected_color)   # Output: (randomly selected color)

5. shuffle(seq):

  • The shuffle() function randomly shuffles the elements in the given sequence seq.

Example:

import random

# Shuffle a list of numbers
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)   # Output: (randomly shuffled list)

QUIZ

1. What is the purpose of the random module in Python?

    1. Generating random numbers and making random selections
    1. Sorting lists in random order
    1. Performing complex mathematical operations
    1. Handling exceptions and errors

2. Which function is used to initialize the random number generator with a specific seed value?

    1. random.seed()
    1. random.randint()
    1. random.random()
    1. random.shuffle()

3. The random.randint(a, b) function returns a random integer between:

    1. a and b (inclusive)
    1. a and b (exclusive)
    1. 0 and b (inclusive)
    1. 0 and 1

4. The random.random() function returns a random float between:

    1. [0.0,1.0]
    1. (0.0,1.0)
    1. [0.0,1.0)
    1. (0.0,1.0]

5. Which function is used to select a random element from a given sequence?

    1. random.random()
    1. random.seed()
    1. random.choice()
    1. random.shuffle()

6. The random.choice(seq) function returns:

    1. A random integer from the given sequence
    1. A random float from the given sequence
    1. A random string from the given sequence
    1. A random element from the given sequence

7. What does the random.seed() function do?

    1. Generates a random seed value
    1. Sets the seed value for the random number generator
    1. Resets the random number generator to its initial state
    1. Generates a random integer

8. Which function is used to randomly shuffle the elements in a sequence?

    1. random.seed()
    1. random.randint()
    1. random.random()
    1. random.shuffle()

9. The random.shuffle(seq) function shuffles the elements in the given sequence:

    1. In ascending order
    1. In descending order
    1. In random order
    1. According to a specific pattern

10. What is the output of random.randint(5, 10)?

    1. A random integer between 1 and 10
    1. A random integer between 0 and 5
    1. A random integer between 5 and 10
    1. A random float between 5 and 10

11. The random.random() function always returns:

    1. An integer
    1. A float between 0 and 1
    1. A float between -1 and 1
    1. A boolean value

12. The random.choice(seq) function can be used with which of the following data types?

    1. Integer
    1. Float
    1. String
    1. All of the above

13. The random.seed() function is commonly used when:

    1. Generating secure random numbers
    1. Repeating the same sequence of random numbers
    1. Generating random strings
    1. Shuffling a list

14. The random.shuffle(seq) function modifies the original sequence:

    1. True
    1. False

15. What is the purpose of setting a seed value for the random number generator?

    1. To generate more random numbers
    1. To make the random numbers predictable
    1. To improve the randomness of the numbers
    1. To avoid errors in the program

16. The random.choice(seq) function can be used to randomly select an element from which of the following?

    1. List
    1. Tuple
    1. String
    1. All of the above

17. Which function is used to generate a random float between 0 and 1 (exclusive)?

    1. random.randint()
    1. random.random()
    1. random.choice()
    1. random.shuffle()

18. What is the purpose of the random.shuffle(seq) function?

    1. To sort the elements in a sequence
    1. To reverse the order of the elements in a sequence
    1. To randomly rearrange the elements in a sequence
    1. To remove duplicate elements from a sequence

19. The random.randint(a, b) function can generate a random integer that includes both a and b.

    1. True
    1. False

20. The random.shuffle(seq) function can be used with sequences other than lists.-

    1. True
    1. False

Exercise 1: Generate a random number using random.seed()

Expand to see the code
def generate_random_number():
    # Set the seed value to 42
    random.seed(42)
    num = random.randint(1, 10)
    return num

print(generate_random_number())  # Output: Random number between 1 and 10 with seed 42

Exercise 2: Generate a list of 5 random integers within a range 1 to 100

Expand to see the code
import random

# Exercise: Generate a list of random integers within a range
def generate_random_list(start, end, size):
    random_list = [random.randint(start, end) for _ in range(size)]
    return random_list

# Test the function
print(generate_random_list(1, 100, 5))  # Output: List of 5 random integers between 1 and 100

Exercise 3: Generate a random floating-point number between two values (random.random)

Expand to see the code
import random

# Exercise: Generate a random floating-point number between two values
def generate_random_float(start, end):
    num = random.random() * (end - start) + start
    return num

# Test the function
print(generate_random_float(0.5, 2.5))  # Output: Random float between 0.5 and 2.5

Exercise 4: Select a random color from a given list [‘red’, ‘blue’, ‘green’, ‘yellow’]

Expand to see the code
import random

# Exercise: Select a random element from a given list
def select_random_element(lst):
    element = random.choice(lst)
    return element

# Test the function
colors = ['red', 'blue', 'green', 'yellow']
print(select_random_element(colors))  # Output: Random element from the colors list

Exercise 5: Shuffle the elements in a given list [1,2,3,4,5]

Expand to see the code
import random

# Exercise: Shuffle the elements in a given list
def shuffle_list(lst):
    random.shuffle(lst)
    return lst

# Test the function
numbers = [1, 2, 3, 4, 5]
print(shuffle_list(numbers))  # Output: Shuffled list of numbers

MCQ ANSWER KEY

Expand to see answers
a) Generating random numbers and making random selections
a) random.seed()
a) a and b (inclusive)
c) [0.0,1.0)
c) random.choice()
d) A random element from the given sequence
b) Sets the seed value for the random number generator
d) random.shuffle()
c) In random order
c) A random integer between 5 and 10
b) A float between 0 and 1
d) All of the above
b) Repeating the same sequence of random numbers
a) True
b) To make the random numbers predictable
d) All of the above
b) random.random()
c) To randomly rearrange the elements in a sequence
a) True
a) True